/*This file concords the different income variables used across the waves in the European Social Survey. It also adds hte Routinene, Offshorability, and Manufacture industry measures.*/

 cd "/Users/dillonlaaker/Dropbox/shocks_replication/data/raw"
 use ess_raw.dta, clear


/*Income*/
mvdecode hinctnt, mv(77 = .r \ 88 = .d \ 99= .n)
label drop hinctnt
label define hinctnt  1 "J" 2 "R" 3 "C" 4 "M" 5 "F" 6 "S" 7 "K" 8 "P" 9 "D" 10 "H" 11 "U" 12 "N" ///
 .r "Refusal" .d "Don't know" .n "No answer"
label val hinctnt hinctnt

*--------------------------
*Euro values for 12 catgory income variables.
*--------------------------
*lower cutoff
gen hinctnt_euros_lo = .
label var hinctnt_euros_lo "Household income lower Euro cut-offs of categories"
local i = 0
foreach num of num 0 1800 3600 6000 12000 18000 24000 30000 36000 60000 90000 120000 {
	local ++i
	recode hinctnt_euros_lo (.=`num') if(hinctnt==`i' & essround<=3)
}

*upper cutoff
gen hinctnt_euros_hi = .
label var hinctnt_euros_hi "Household income lower Euro cut-offs of categories"
local i = 0
foreach num of num 1799 3599 5999 11999 17999 23999 29999 35999 59999 89999 111999 150000{
	local ++i
	recode hinctnt_euros_hi (.=`num') if(hinctnt==`i' & essround<=3)
}

*--------------------------
*Imputation of the "Income in Euros"- variable using  uniform random values between
* the upper and lower cut-offs
*--------------------------
gen income_random = hinctnt_euros_lo+(hinctnt_euros_hi-hinctnt_euros_lo)*runiform()
label var income_random "Household income in Euro: random values between cut-offs"

*--------------------------
*Variables containing the upper decile cut-offs
*by country. Each var = one decile.
*--------------------------
sort cntry essround
foreach num of num 10(10)90{
	by cntry essround:  /// <--Ensures that the income distribution is by country/year.
			egen income_q`num' = pctile(income_random) ///
			if(essround<=3) , p(`num')
}

*--------------------------
*Generate assign individuals to deciles.
*--------------------------
gen hinctnta_imputed =.
label var hinctnta_imputed "Household income deciles - imputed"

recode hinctnta_imputed (.=1) if(income_random<income_q10)
recode hinctnta_imputed (.=2) if(income_random>=income_q10 & income_random<income_q20)
recode hinctnta_imputed (.=3) if(income_random>=income_q20 & income_random<income_q30)
recode hinctnta_imputed (.=4) if(income_random>=income_q30 & income_random<income_q40)
recode hinctnta_imputed (.=5) if(income_random>=income_q40 & income_random<income_q50)
recode hinctnta_imputed (.=6) if(income_random>=income_q50 & income_random<income_q60)
recode hinctnta_imputed (.=7) if(income_random>=income_q60 & income_random<income_q70)
recode hinctnta_imputed (.=8) if(income_random>=income_q70 & income_random<income_q80)
recode hinctnta_imputed (.=9) if(income_random>=income_q80 & income_random<income_q90)
recode hinctnta_imputed (.=10) if(income_random>=income_q90)

replace hinctnta_imputed=. if essround==4
replace hinctnta_imputed=. if essround==5
replace hinctnta_imputed=. if essround==6
replace hinctnta_imputed=. if essround==7
replace hinctnta_imputed=. if essround==8

gen income=.
replace income=1 if hinctnta_imputed==1
replace income=2 if hinctnta_imputed==2
replace income=3 if hinctnta_imputed==3
replace income=4 if hinctnta_imputed==4
replace income=5 if hinctnta_imputed==5
replace income=6 if hinctnta_imputed==6
replace income=7 if hinctnta_imputed==7
replace income=8 if hinctnta_imputed==8
replace income=9 if hinctnta_imputed==9
replace income=10 if hinctnta_imputed==10
replace income=1 if hinctnta==1
replace income=2 if hinctnta==2
replace income=3 if hinctnta==3
replace income=4 if hinctnta==4
replace income=5 if hinctnta==5
replace income=6 if hinctnta==6
replace income=7 if hinctnta==7
replace income=8 if hinctnta==8
replace income=9 if hinctnta==9
replace income=10 if hinctnta==10

drop hinctnt_euros_lo hinctnt_euros_hi income_random income_q10 income_q20 income_q30 income_q40 income_q50 income_q60 income_q70 income_q80 income_q90 hinctnta_imputed

gen country=""
replace country="austria" if cntry=="AT"
replace country="belgium" if cntry=="BE"
replace country="bulgaria" if cntry=="BG"
replace country="croatia" if cntry=="HR"
replace country="czech" if cntry=="CZ"
replace country="denmark" if cntry=="DK"
replace country="estonia" if cntry=="EE"
replace country="finland" if cntry=="FI"
replace country="france" if cntry=="FR"
replace country="germany" if cntry=="DE"
replace country="greece" if cntry=="GR"
replace country="hungary" if cntry=="HU"
replace country="italy" if cntry=="IT"
replace country="netherlands" if cntry=="NL"
replace country="norway" if cntry=="NO"
replace country="portugal" if cntry=="PT"
replace country="russia" if cntry=="RU"
replace country="slovenia" if cntry=="SI"
replace country="spain" if cntry=="ES"
replace country="sweden" if cntry=="SE"
replace country="switzerland" if cntry=="CH"
replace country="uk" if cntry=="GB"
replace country="iceland" if cntry=="IS"

drop if missing(country)
replace yrbrn=. if yrbrn<1900
drop if missing(yrbrn)

gen datalim=0
replace datalim=. if yrbrn<1924 & country=="bulgaria" 
replace datalim=. if yrbrn<1924 & country=="hungary"
replace datalim=. if yrbrn<1952 & country=="croatia" 
replace datalim=. if yrbrn<1970 & country=="czech" 
replace datalim=. if yrbrn<1980 & country=="estonia" 
replace datalim=. if yrbrn<1952 & country=="slovenia" 
 
drop if missing(datalim)

drop datalim
gen datalim=0
replace datalim=1 if country=="ireland" 
replace datalim=1 if country=="bulgaria" 
replace datalim=1 if country=="hungary" 
replace datalim=1 if country=="croatia" 
replace datalim=1 if country=="czech" 
replace datalim=1 if country=="estonia" 
replace datalim=1 if country=="slovenia" 


save ess_incomefixed.dta, replace

